Question #1

 

Given the following class definitions, write down the output of the following program segment and explain each line of output:

 

   class C {
      public:
         C() {
           cout << "C::C()" << endl;
         }
     
         C(int) {
           cout << "C::C(int)" << endl;
         }
 
         C(const C&) {
           cout << "C::C(const C&)" << endl;
         }
 
         C& operator=(const C&) {
           cout << "C::operator=(const C&)" << endl;
           return *this;
         }
 
         ~C() {
           cout << "C::~C()" << endl;
         }
   };
 
 
   C func(C c1) {
      C c2(5);
      
      c2 = c1;
      return c2;
   }
 
   CODE SEGMENT:
     ...
     {
       C c1,c2;
       c2 = func(c1);
     }
     ...

Question #2

Consider the following definitions. Give the OUTPUT of the following program segment, and briefly explain what caused the line to be printed

 
      int A = 3;
 
      class Base {
         public:
            Base() {
               A = A + 2;
               cout << "Base::Base()" << endl;
            }
 
            Base(const Base&) {
               A = A - 1;
               cout << "Base::Base(const Base&)" << endl;
            }
 
            ~Base() {
               A = 2*A;
               cout << "Base::~Base()" << endl;
            }
      };
 
      class Derived: public Base {
         public:
            Derived() {
               A = 3*A;
               cout << "Derived::Derived()" << endl;
            }
 
            Derived(const Derived& d):Base(d) {
               A = A - 2;
               cout << "Derived::Derived(const Derived&)" << endl;
            }
 
            ~Derived() {
               A = A + 4;
               cout << "Derived::~Derived" << endl;
            }
      };
 
   ....
   {
    Derived d1, d2(d1);
   }
   cout << "A = " << A << endl;
   ...
 
 
 

Question #3

Given the following definitions for class stack, implement methods push, pop and empty:

   struct stack_node {
      int element;
      stack_node* next;
   };
 
   class stack {
      // LINKED STACK:
      //    stack S; 
      //    S.push(3); S.push(5); S.push(2); -->
      //
      //    S.top->element = 2
      //         ->next    -> element = 5
      //                   -> next    -> element = 3
      //                              -> next    = NULL;
      //    S.pop() == 2
      //    S.pop() == 5
      //    S.pop() == 3
      //    S.empty() == true
      private:
         stack_node * top;
 
      public:
         stack():top(NULL) {
         }
 
         ~stack() {
            while(!empty()) pop(); 
         }
 
         void push(int);      // YOU IMPLEMENT IT!
         int pop();           // YOU IMPLEMENT IT!
         bool empty() const;  // YOU IMPLEMENT IT!
    };
 
 

Question 4

Implement a string class. The Sting class is a wrapper for a char*. Implement the copy constructor (1), destructor (2) and method length (3). Overload operator<< (4) and operator+ (5). For strings

      s1("hello"), s2(" world") s3 = s1 + s2;
   

s3 becomes "hello world", while s1 and s2 remain unchanged. operator<< prints the string to an output stream. The implementation of the default constructor, should help you understand how an instance of class String is represented. You may use:

    
    int strlen(char* s);             // returns the length of s
    void strcpy(char* s1, char* s2); // copies the characters from
                                     // s2 into s1
    
 
     class String {
        private:
           char* s;  // 0 byte terminated, dynamic array of characters
        public:
            String(char* =NULL);    // default and one argument constructor
 
            // IMPLEMENT THE METHODS BELOW:
            String(const String&);  // copy constructor
            // return a new string, which is obtained by
            // appending the argument string to a copy of s
            String operator+(const String&) const;
            ~String();              // destructor
            int length() const;     // length of string
 
        friend ostream& operator<<(ostream&,const String&);
     };
 
 
     String::String(char* str) {
        if (str == NULL) {
           s = str;
           return;
        }
        s = new char[strlen(str)+1];
        strcpy(s,str);
     }